//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package extension;

import cucumber.api.event.TestRunFinished;
import cucumber.api.formatter.Formatter;
import cucumber.runtime.ClassFinder;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.RuntimeOptionsFactory;
import cucumber.runtime.formatter.PluginFactory;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;
import cucumber.runtime.junit.*;
import cucumber.runtime.junit.FeatureRunner;
import cucumber.runtime.junit.JUnitOptions;
import cucumber.runtime.junit.JUnitReporter;
import cucumber.runtime.model.CucumberFeature;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.ParentRunner;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;

public class Cucumber extends ParentRunner<FeatureRunner> {
    private final JUnitReporter jUnitReporter;
    private final List<FeatureRunner> children = new ArrayList();
    private final Runtime runtime;
    private final Formatter formatter;

    public Cucumber(Class clazz) throws InitializationError, IOException {
        super(clazz);
        ClassLoader classLoader = clazz.getClassLoader();
        Assertions.assertNoCucumberAnnotatedMethods(clazz);

        EvnsFactory evnsFactory = new EvnsFactory(clazz);
        Evns evns = evnsFactory.create();
        evns.init();

        RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(clazz);
        RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
//        try {
//            Class<RuntimeOptions> optionsClass = RuntimeOptions.class;
//            Field field = optionsClass.getDeclaredField("pluginFactory");
//            field.setAccessible(true);
//            PluginFactory pluginFactory = (PluginFactory) field.get(runtimeOptions);
//            Class<PluginFactory> pluginFactoryClass = PluginFactory.class;
//            Field pluginFactoryField = pluginFactoryClass.getDeclaredField("PLUGIN_CLASSES");
//            pluginFactoryField.setAccessible(true);
//            Map<String, Class> PLUGIN_CLASSES = (Map<String, Class>) pluginFactoryField.get(pluginFactory);
//            PLUGIN_CLASSES.put("return", LocalRerunFormatter.class);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        ResourceLoader resourceLoader = new MultiLoader(classLoader);
        this.runtime = this.createRuntime(resourceLoader, classLoader, runtimeOptions);
        this.formatter = runtimeOptions.formatter(classLoader);
        JUnitOptions junitOptions = new JUnitOptions(runtimeOptions.getJunitOptions());
        List<CucumberFeature> cucumberFeatures = runtimeOptions.cucumberFeatures(resourceLoader, this.runtime.getEventBus());
        this.jUnitReporter = new JUnitReporter(this.runtime.getEventBus(), runtimeOptions.isStrict(), junitOptions);
        this.addChildren(cucumberFeatures);
    }

    /** @deprecated */
    @Deprecated
    protected Runtime createRuntime(ResourceLoader resourceLoader, ClassLoader classLoader, RuntimeOptions runtimeOptions) throws InitializationError, IOException {
        ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
        return new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
    }

    public List<FeatureRunner> getChildren() {
        return this.children;
    }

    protected Description describeChild(FeatureRunner child) {
        return child.getDescription();
    }

    protected void runChild(FeatureRunner child, RunNotifier notifier) {
        child.run(notifier);
    }

    protected Statement childrenInvoker(RunNotifier notifier) {
        final Statement features = super.childrenInvoker(notifier);
        return new Statement() {
            public void evaluate() throws Throwable {
                features.evaluate();
                Cucumber.this.runtime.getEventBus().send(new TestRunFinished(Cucumber.this.runtime.getEventBus().getTime()));
                Cucumber.this.runtime.printSummary();
            }
        };
    }

    private void addChildren(List<CucumberFeature> cucumberFeatures) throws InitializationError {
        Iterator var2 = cucumberFeatures.iterator();

        while(var2.hasNext()) {
            CucumberFeature cucumberFeature = (CucumberFeature)var2.next();
            FeatureRunner featureRunner = new FeatureRunner(cucumberFeature, this.runtime, this.jUnitReporter);
            if(!featureRunner.isEmpty()) {
                this.children.add(featureRunner);
            }
        }

    }
}
